#include "common.h"

struct v2p {
float4 tc0:TEXCOORD0;
float4 tc1:TEXCOORD1;
float4 tc2:TEXCOORD2;
float4 tc3:TEXCOORD3;
float4 tc4:TEXCOORD4;
float4 tc5:TEXCOORD5;
float4 tc6:TEXCOORD6;
};

#include "blur.h"

struct vf
{
float4 hpos:POSITION;
float2 tbase:TEXCOORD0;
float2 tnorm0:TEXCOORD1;
float2 tnorm1:TEXCOORD2;
half3 M1:TEXCOORD3;
half3 M2:TEXCOORD4;
half3 M3:TEXCOORD5;
half3 v2point:TEXCOORD6;
float4 tctexgen:TEXCOORD7;
half4 c0:COLOR0;
};
uniform sampler2D	s_nmap;
uniform samplerCUBE	s_env0;
uniform samplerCUBE	s_env1;
uniform sampler2D	s_leaves;

uniform half4 e_barrier;

#if defined(USE_SOFT_WATER) && defined(NEED_SOFT_WATER)
half3	water_intensity;
#endif
/*
half4 main (v2p I):COLOR
{
half4 t_base=tex2D (s_base,I.tbase);
half3 t_env=texCUBE (s_env, I.tenv);
half3 refl=lerp (t_env,t_base,I.c0.a);
half3 color=lerp (refl, t_base,t_base.a);
half3 final=color*I.c0*2;
half alpha_shift=saturate(.5-I.c0.a);
half alpha_add=alpha_shift*alpha_shift;
half alpha=t_base.a;
return half4 (final, t_base.a);
}
*/
half4 main_ps_2_0 (vf I):COLOR
{
half4	base	=tex2D (s_base,I.tbase);
half3	n0	=tex2D (s_nmap,I.tnorm0);
half3	n1	=tex2D (s_nmap,I.tnorm1);
half3	Navg	=n0+n1-1;
half3	Nw	=mul (half3x3(I.M1, I.M2, I.M3), Navg);
Nw	=normalize (Nw);
half3	v2point	=normalize (I.v2point);
half3	vreflect=reflect(v2point, Nw);
vreflect.y=vreflect.y*2-1;
/*
half3 vreflectabs=abs(vreflect);
half vreflectmax=max(vreflectabs.x, max(vreflectabs.y, vreflectabs.z));
vreflect/=vreflectmax;
if (vreflect.y<0.99)
vreflect.y=vreflect.y*2-1;
*/
half3	env0	=texCUBE (s_env0, vreflect);
half3	env1	=texCUBE (s_env1, vreflect);
half3	env	=lerp (env0,env1,L_ambient.w);
env	*=env*2;
half	fresnel	=saturate (dot(vreflect,v2point));
half	power	=pow(fresnel,9);
half	amount	=0.15h+0.25h*power;
half3	c_reflection=env*amount;
half3	final=lerp(c_reflection,base.rgb,base.a);
final	*=I.c0*2;
#ifdef USE_VTF
#else
#endif
#ifdef	NEED_SOFT_WATER
half	alpha	=0.75h+0.25h*power;
#ifdef	USE_SOFT_WATER
half4 _P=tex2Dproj(s_position, I.tctexgen);
half waterDepth=_P.z-I.tctexgen.z;
half fog_exp_intens=-4.0h;
float fog	=1-exp(fog_exp_intens*waterDepth);
half3 Fc=half3(0.1h, 0.1h, 0.1h)*water_intensity.r;
final=lerp (Fc, final, alpha);
alpha=min(alpha, saturate(waterDepth));
alpha=max (fog, alpha);
half4	leaves	=tex2D(s_leaves, I.tbase);
half	calc_cos=dot (I.M3, v2point);
half	calc_depth=saturate(waterDepth*calc_cos);
half	fLeavesFactor=smoothstep(0.025, 0.05, calc_depth);
fLeavesFactor*=smoothstep(0.1, 0.075, calc_depth);
final	=lerp(final, leaves, leaves.a*fLeavesFactor);
alpha	=lerp(alpha, leaves.a, leaves.a*fLeavesFactor);
#endif

// BLUR by Meltac
//float3 sum=tex2D(s_image,I.tbase).rgb;
//float samples = DOF_QUALITY * DOF_OVERSAMPLING;
//float blur = 1.0f;
//sum = classicblur(I.tbase, sum, MAXCOF * blur, samples);
//float contrib = samples;
//final += sum /contrib;

return half4 (final, alpha*I.c0.a*I.c0.a);
#else

// BLUR by Meltac
//float3 sum=tex2D(s_image,I.tbase).rgb;
//float samples = DOF_QUALITY * DOF_OVERSAMPLING;
//float blur = 1.0f;
//sum = classicblur(I.tbase, sum, MAXCOF * blur, samples);
//float contrib = samples;
//final += sum /contrib;

return half4 (final, 1);
#endif
}